package com.nuoniu.sibanyun.controller.erp;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.ErpNumberSetSearchDto;
import com.nuoniu.sibanyun.entity.erp.ErpNumberSet;
import com.nuoniu.sibanyun.service.erp.IErpNumberSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 单据编号设置  前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-11-02
 */
@RestController
@RequestMapping("/erp/numberSet")
@Api(tags = "单据编号设置")
public class ErpNumberSetController extends BaseController {

    @Autowired
    private IErpNumberSetService numberSetService;

    /**
     * 编号列表
     * @author 小啊刚
     * @date 2020-11-02
     */
    @GetMapping("pageList")
    public HttpResult pageList(Page page, ErpNumberSet numberSet) {
        return HttpResult.ok(numberSetService.pageList(page, numberSet));
    }

    /**
     * 保存编号
     * @author 小啊刚
     * @date 2020-11-02
     */
    @PostMapping("save")
    public HttpResult save(@RequestBody ErpNumberSet numberSet) {
        if(StringUtils.isBlank(numberSet.getCurrentCode())) {
            throw new NuoNiuException("默认前缀不能为空");
        }

        if(numberSet.getStreamNumber() == null || numberSet.getStreamNumber() < 4) {
            throw new NuoNiuException("流水位数必须大于等于4位");
        }

        if(numberSet.getStreamNumber() > 9) {
            throw new NuoNiuException("流水位数不能超过9位");
        }

        if(numberSet.getStartValue() == null || numberSet.getStartValue().toString().length() > numberSet.getStreamNumber()) {
            throw new NuoNiuException("初始值位数不能大于流水位");
        }

        numberSetService.saveNumberSet(numberSet);
        return HttpResult.ok();
    }

    /**
     * 当前账套的 单据编码集合
     * @return
     */
    @PostMapping("list")
    public HttpResult list(@RequestBody ErpNumberSetSearchDto erpNumberSetSearchDto) {
        return HttpResult.ok(numberSetService.listErpNumberSet(erpNumberSetSearchDto));
    }

    /**
     * 当前账套的 单据编码集合
     * @return
     */
    @GetMapping("parentList")
    public HttpResult parentList(String moduleCode) {
        return HttpResult.ok(numberSetService.listErpNumberParentList(moduleCode));
    }

    /**
     * 删除
     * @author 小啊刚
     * @date 2021-10-20
     */
    @ApiOperation("删除")
    @DeleteMapping("deleteById")
    public HttpResult deleteById(Long id) {
        numberSetService.deleteById(id);
        return HttpResult.ok();
    }
}
